package com.music.albums.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.music.client.albums.client.AlbumClient;
import com.music.client.folders.client.FolderClient;
import com.music.service.albums.beans.Album;
import com.music.service.folders.beans.Folder;

public class AlbumHomeController implements Controller {

	private AlbumClient mAlbumClient;
	
	private FolderClient mFolderClient;
	
	private String mViewName;
	
	public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) 
					throws ServletException, IOException {
		
		ModelAndView modelAndView = new ModelAndView();
		Long memberId = Long.valueOf(request.getParameter("memberId"));
		
		if(memberId == null){
			return new ModelAndView("error.jsp");
		}
		
		List<Folder> folders = getFolderClient().viewActiveFolders(memberId);
		List<Album> albums = getAlbumClient().getDefaultAlbums(memberId);
		
		modelAndView.addObject("albums",albums);
		modelAndView.addObject("folders",folders);
		modelAndView.setViewName(getViewName());
		
		return modelAndView;
	}

	/**
	 * @return the albumClient
	 */
	public AlbumClient getAlbumClient() {
		return mAlbumClient;
	}

	/**
	 * @param pAlbumClient the albumClient to set
	 */
	@Required
	public void setAlbumClient(AlbumClient pAlbumClient) {
		mAlbumClient = pAlbumClient;
	}

	/**
	 * @return the folderClient
	 */
	public FolderClient getFolderClient() {
		return mFolderClient;
	}

	/**
	 * @param pFolderClient the folderClient to set
	 */
	@Required
	public void setFolderClient(FolderClient pFolderClient) {
		mFolderClient = pFolderClient;
	}

	/**
	 * @return the viewName
	 */
	public String getViewName() {
		return mViewName;
	}

	/**
	 * @param pViewName the viewName to set
	 */
	@Required
	public void setViewName(String pViewName) {
		mViewName = pViewName;
	}

	
}
